package com.imau.service;

import com.imau.bean.User;
import com.imau.mapper.UserMapper;
import com.imau.util.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;

public class UserService {


    //调用持久层
    private SqlSession session;
    private UserMapper userMapper;


    //构造方法初始化

    public UserService()
    {
        session = SqlSessionUtil.getSqlSession();
        userMapper = session.getMapper(UserMapper.class);
    }

    /**
     *
     * @param userName
     * @param password
     * @return
     */
    public User checkLogin(String userName,String password)
    {

        return   userMapper.selectByUserNameAndPassowrd(userName,password);
    }

    /**
     * 检查用户名是否已经存在
     * @param userName
     * @return true 用户名不存在
     */
    public boolean checkUserName(String userName){

        User user = userMapper.selectByUserName(userName);

        return  user == null;

    }

    /**
     * 注册用户
     * @param user
     * @return true 注册成功
     */
    public boolean registerUser(User user)
    {
          if(checkUserName(user.getUserName()))
          {
              return  userMapper.insertUser(user) > 0;
          }

          return  false;
    }
}
